package com.example.news.fraction;

import com.example.news.ResourceTable;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import java.util.ArrayList;
import java.util.List;

public class HomeFraction extends Fraction {
    private TabList tabList;
    private String[] category = {"头条", "新闻", "国内", "国际", "政治", "财经", "体育", "娱乐", "军事", "教育", "科技", "NBA", "股票", "星座", "女性", "健康", "育儿"};
    List<Fraction> fractionList = new ArrayList<>();
    private StackLayout stackLayout2;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {

        Component component = scatter.parse(ResourceTable.Layout_fraction_home, container, false);
        return component;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        initView();

        if (tabList != null) {
            for (int i = 0; i < category.length; i++) {
                TabList.Tab tab = tabList.new Tab(getContext());
                tab.setText(category[i]);
                tabList.addTab(tab);
            }

        }
        getFractionAbility().getUITaskDispatcher().asyncDispatch(new Runnable() {
            @Override
            public void run() {
                loadFraction(0);

            }
        });

        tabList.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
//                fractionList.add(new TouTiao());
                loadFraction(tab.getPosition());
            }

            @Override
            public void onUnselected(TabList.Tab tab) {

            }

            @Override
            public void onReselected(TabList.Tab tab) {

            }
        });


    }

    private void loadFraction(int position) {
        switch (position) {
            case 0:
                getFractionAbility().getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_stacklayout2, new TouTiao())
                        .submit();
                break;
            case 1:
                getFractionAbility().getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_stacklayout2, new XinWen())
                        .submit();
                break;
            case 2:
                getFractionAbility().getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_stacklayout2, new GuoNei())
                        .submit();
                break;
            case 3:
                getFractionAbility().getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_stacklayout2, new GuoJi())
                        .submit();
                break;
            case 4:
                getFractionAbility().getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_stacklayout2, new ZhengZhi())
                        .submit();
                break;
            case 5:
                getFractionAbility().getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_stacklayout2, new CaiJing())
                        .submit();
                break;
            case 6:
                getFractionAbility().getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_stacklayout2, new TiYu())
                        .submit();
                break;
            case 7:
                getFractionAbility().getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_stacklayout2, new YuLe())
                        .submit();
                break;
            case 8:
                getFractionAbility().getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_stacklayout2, new JunShi())
                        .submit();
                break;
            case 9:
                getFractionAbility().getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_stacklayout2, new JiaoYu())
                        .submit();
                break;
            case 10:
                getFractionAbility().getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_stacklayout2, new KeJi())
                        .submit();
                break;
            case 11:
                getFractionAbility().getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_stacklayout2, new NBA())
                        .submit();
                break;
            case 12:
                getFractionAbility().getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_stacklayout2, new GuPiao())
                        .submit();
                break;
            case 13:
                getFractionAbility().getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_stacklayout2, new XingZuo())
                        .submit();
                break;
            case 14:
                getFractionAbility().getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_stacklayout2, new NvXing())
                        .submit();
                break;
            case 15:
                getFractionAbility().getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_stacklayout2, new JianKang())
                        .submit();
                break;
            case 16:
                getFractionAbility().getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_stacklayout2, new YuEr())
                        .submit();
                break;
            default:
                break;
        }
    }

    private void initView() {
        tabList = getFractionAbility().findComponentById(ResourceTable.Id_tablist_category);
        stackLayout2 = getFractionAbility().findComponentById(ResourceTable.Id_stacklayout2);

    }
}
